package ui

import (
	"alist-gui/alist"
	"alist-gui/res"
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"image/color"
	"net/url"
)

type Alist struct {
	widget.BaseWidget

	start   *widget.Button
	restart *widget.Button
	stop    *widget.Button
	storage *widget.Button

	resetPassword *widget.Button
	admin         *widget.Button

	version *widget.Label

	outLabel  *widget.Label
	outScroll *container.Scroll

	cmd *alist.CmdAlist
}

func (a *Alist) CreateRenderer() fyne.WidgetRenderer {
	var objs []fyne.CanvasObject

	objs = append(objs, a.start)
	objs = append(objs, a.restart)
	objs = append(objs, a.stop)
	objs = append(objs, a.storage)
	objs = append(objs, a.resetPassword)
	objs = append(objs, a.admin)

	objs = append(objs, a.version)

	objs = append(objs, a.outScroll)

	// return widget.NewSimpleRenderer(container.New(&AlistLayout{a: a}, objs...))
	backgroud := canvas.NewRectangle(color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x20}) // 白色半透明
	index := widget.NewHyperlink("主页", parseURL("http://localhost:5244"))

	return widget.NewSimpleRenderer(container.NewBorder(container.NewHBox(a.version, index),
		container.NewVBox(
			container.NewHBox(a.start, a.restart, a.stop),
			container.NewHBox(a.storage, a.resetPassword, a.admin),
		), nil, nil,
		a.outScroll, backgroud))
}

func NewAlistUi(pa fyne.Window) (*Alist, func(), error) {
	cmd, err := alist.NewCmdAlist()
	if err != nil {
		return nil, nil, err
	}
	a := &Alist{
		cmd:      cmd,
		outLabel: widget.NewLabel(""),
	}

	a.outLabel.Importance = widget.HighImportance
	a.outLabel.Wrapping = fyne.TextTruncate
	a.outScroll = container.NewVScroll(a.outLabel)

	a.outLabel.SetText(cmd.Version())

	a.version = widget.NewLabel(fmt.Sprintf("Alist %s", res.Version))

	a.start = widget.NewButtonWithIcon("开启", theme.LoginIcon(), func() {
		a.outLabel.SetText(a.cmd.Start())
		a.outScroll.ScrollToTop()
	})
	a.stop = widget.NewButtonWithIcon("停止", theme.ContentClearIcon(), func() {
		a.outLabel.SetText(a.cmd.Stop())
		a.outScroll.ScrollToTop()
	})
	a.restart = widget.NewButtonWithIcon("重启", theme.ContentRedoIcon(), func() {
		a.outLabel.SetText(a.cmd.Restart())
		a.outScroll.ScrollToTop()
	})
	a.storage = widget.NewButtonWithIcon("存储", theme.DocumentSaveIcon(), func() {
		a.outLabel.SetText(a.cmd.Storage())
		a.outScroll.ScrollToTop()
	})
	a.admin = widget.NewButtonWithIcon("查看用户", theme.AccountIcon(), func() {
		a.outLabel.SetText(a.cmd.Admin())
		a.outScroll.ScrollToTop()
	})

	a.resetPassword = widget.NewButtonWithIcon("修改密码", theme.SettingsIcon(), func() {
		dialog.NewEntryDialog("修改密码", "新密码", func(s string) {
			a.outLabel.SetText(a.cmd.ResetPassword(s))
			a.outScroll.ScrollToTop()
		}, pa).Show()
	})

	a.ExtendBaseWidget(a)

	// 关闭时执行
	cancel := func() {
		a.cmd.Stop()
	}
	return a, cancel, nil
}

func parseURL(urlStr string) *url.URL {
	link, err := url.Parse(urlStr)
	if err != nil {
		fyne.LogError("Could not parse URL", err)
	}
	return link
}
